package com.quan.system.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.quartz.CronScheduleBuilder;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.quan.common.bean.MailBean;
import com.quan.common.bean.R;
import com.quan.common.core.MyBaseController;
import com.quan.common.utils.DateUtils;
import com.quan.system.common.amqp.producer.ProducerMessageSendMail;

/**
 * <p>
 * 地区表 前端控制器
 * </p>
 *
 * @author 杨浩泉
 * @since 2019-06-05
 */
@RestController
@RequestMapping("/system/test")
public class SystemTestController extends MyBaseController {

	@Autowired
	private Scheduler scheduler;
	
	@Autowired
	private ProducerMessageSendMail producerMessageSendMail;

	/**
	 * 新增一个定时作业
	 * 
	 * @return
	 */
	@GetMapping("/quartz/add")
	@SuppressWarnings("unchecked")
	public Object addJob() {
		try {
			logger.debug("添加任务并启动.......");

			Class<Job> clz = (Class<Job>) Class.forName("com.quan.system.common.job.TestJob");

			// 构建JOB信息
			JobDetail job = JobBuilder.newJob(clz).withIdentity("jobName-001", "jobGroup-001")
					.withDescription("jobDesc-001").build();

			// 触发时间点
			CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
			Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger-jobName-001", "jobGroup-001").startNow()
					.withSchedule(cronScheduleBuilder).build();

			// 交由Scheduler安排触发
			Date scheduleJob = scheduler.scheduleJob(job, trigger);
			System.out.println(scheduleJob);
			System.out.println(DateUtils.formatDateTime(scheduleJob));

		} catch (Exception e) {
			e.printStackTrace();
		}

		return R.ok();
	}

	/**
	 * 暂停一个定时作业
	 * 
	 * @return
	 */
	@GetMapping("/quartz/pause")
	public Object pauseJob() {
		try {
			logger.debug("暂停任务.......");
			
			JobKey key = new JobKey("jobName-001", "jobGroup-001");
			scheduler.pauseJob(key);
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		}

		return R.ok();
	}

	/**
	 * 恢复一个定时作业
	 * 
	 * @return
	 */
	@GetMapping("/quartz/resume")
	public Object resumeJob() {
		try {
			logger.debug("恢复任务.......");
			
			JobKey key = new JobKey("jobName-001", "jobGroup-001");
			scheduler.resumeJob(key);
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		}

		return R.ok();
	}

	/**
	 * 删除一个定时作业
	 * 
	 * @return
	 */
	@GetMapping("/quartz/delete")
	public Object deleteJob() {
		try {
			logger.debug("删除任务.......");
			
			//暂停任务
			JobKey key = new JobKey("jobName-001", "jobGroup-001");
			scheduler.pauseJob(key);
			
			TriggerKey triggerKey = TriggerKey.triggerKey("jobName-001", "jobGroup-001");  
			// 停止触发器  
			scheduler.pauseTrigger(triggerKey);  
			// 移除触发器  
			scheduler.unscheduleJob(triggerKey);  
			
			// 删除任务  
			scheduler.deleteJob(key);  
			System.out.println("triggerKey:"+triggerKey.toString());
			System.out.println("removeJob:"+key.toString());  
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
		return R.ok();
	}
	
	
	@GetMapping("/sendMail")
	public Object sendMail() {
		try {
			logger.debug("发送邮件.......");
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("subject", "测试邮件发送");
			map.put("realname", "测试");
			map.put("username", "1234");
			map.put("password", "123456");
			
			MailBean mailBean = new MailBean();
			mailBean.setTo("yanghaoquan@whizen.com");
			mailBean.setSubject(map.get("subject").toString());
			mailBean.setTemplateLocation("mail/account.html");
			mailBean.setTemplateData(map);
			
			this.producerMessageSendMail.sender(mailBean);  
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok();
	}

}
